package com.bjpowernode.crm.settings.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.dto.UserLoginDto;
import com.bjpowernode.crm.settings.mapper.UserMapper;
import com.bjpowernode.crm.settings.service.UserService;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class UserServiceImpl implements UserService {

    /**
     * 访问用户数据
     */
    private UserMapper userMapper;

    @Override
    public ReturnObject login(UserLoginDto userLoginDto, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ReturnObject returnObject = new ReturnObject();

        User user = this.userMapper.selectUserByLoginAct(userLoginDto.getLoginAct());

        if (user == null) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
            return returnObject;
        }

        if (userLoginDto.getLoginPwd() == null || !userLoginDto.getLoginPwd().equals(user.getLoginPwd())) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
            return returnObject;
        }

        if (DateUtils.formatDateTime(new Date()).compareTo(user.getExpireTime()) > 0) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("账号已过期");
            return returnObject;
        }

        if ("0".equals(user.getLockState())) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("状态被锁定");
            return returnObject;
        }

        if (!user.getAllowIps().contains(request.getRemoteAddr())) {
//            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
//            returnObject.setMessage("ip受限");
//            return returnObject;
        }

        //认证通过
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，登录成功");

        session.setAttribute(Contants.SESSION_USER, user);

        //记住密码
        Cookie cookieOfLoginAct = new Cookie("loginAct", user.getLoginAct());
        Cookie cookieOfLoginPwd = new Cookie("loginPwd", user.getLoginPwd());

        if (userLoginDto.getIsRemPwd() != null && userLoginDto.getIsRemPwd()) {//记住密码
            int expiry = 10 * 24 * 60 * 60;//10 天后过期
            cookieOfLoginAct.setMaxAge(expiry);
            cookieOfLoginPwd.setMaxAge(expiry);
        } else {//不记住密码
            cookieOfLoginAct.setMaxAge(0);
            cookieOfLoginPwd.setMaxAge(0);
        }

        response.addCookie(cookieOfLoginAct);
        response.addCookie(cookieOfLoginPwd);
        return returnObject;
    }

    @Override
    public void logout(HttpServletResponse response, HttpSession session) {
        Cookie cookieOfLoginAct = new Cookie("loginAct", "1");
        cookieOfLoginAct.setMaxAge(0);
        response.addCookie(cookieOfLoginAct);

        Cookie cookieOfLoginPwd = new Cookie("loginPwd", "1");
        cookieOfLoginPwd.setMaxAge(0);
        response.addCookie(cookieOfLoginPwd);

        session.invalidate();
    }

    @Override
    public List<User> queryAllUsers() {
        return this.userMapper.selectAllUsers();
    }

    @Override
    public List<String> queryIdsByName(String name) {
        return this.userMapper.selectIdsByName(name);
    }

    @Override
    public String queryNameById(String id) {
        return this.userMapper.selectNameById(id);
    }

}
